﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SelectCheckPointPanel : MonoBehaviour
{
    int totalPlayersNumber = 4;
    public List<int> List_index;
    List<ChefData> chef_list;

    public Text Text_TotalStarNumber;
    public Text Text_Level;
    public Text Text_Score;
    public Text Text_StarNumber1;
    public Text Text_StarNumber2;
    public Text Text_StarNumber3;
    public Image Image_Screenshot;
    public Button Button_Close;
    public Button Button_Start;

    public List<Button> List_Button_leftArrow;
    public List<Button> List_Button_rightArrow;
    public List<Image> List_Image_chefHead;


    public LevelData levelData;


    void Awake()
    {
        chef_list = DataManager.GetInstance().m_chefList.chef_list;
        
        for (int i = 0; i < totalPlayersNumber; i++)
        {
            ButtonParam param = new ButtonParam();
            param.playerIndex = i;
            param.dir = -1;
            EventTriggerListener.Get(List_Button_leftArrow[i].gameObject, param).onClick = onClick_ButtonArrow;

            param.dir = 1;
            EventTriggerListener.Get(List_Button_rightArrow[i].gameObject, param).onClick = onClick_ButtonArrow;
        }

        EventTriggerListener.Get(Button_Close.gameObject, null).onClick = onClick_Button_Close;
        EventTriggerListener.Get(Button_Start.gameObject, null).onClick = onClick_Button_Start;

    }

    void OnEnable()
    {
        print("OnEnable");

        for (int i = 0; i < totalPlayersNumber; i++)
        {
            int playerIndex = i;
            Image image = List_Image_chefHead[playerIndex];
            List_index[playerIndex] = Random.Range(0, chef_list.Count);
            ChefData chefData = chef_list[List_index[playerIndex]];
            string path = chefData.head_icon;
            ChangeChef(image, path);
        }
    }

    void OnDisable()
    {
        print("OnDisable");
    }

    //左右按钮更改厨师
    void onClick_ButtonArrow(GameObject go, object data = null)
    {
        ButtonParam param = (ButtonParam)data;

        int playerIndex = param.playerIndex;
        Image image = List_Image_chefHead[playerIndex];

        if(param.dir == -1)
        {
            List_index[playerIndex] = GetIndex(List_index[playerIndex] - 1, 0, chef_list.Count - 1);
        }
        else if(param.dir == 1)
        {
            List_index[playerIndex] = GetIndex(List_index[playerIndex] + 1, 0, chef_list.Count - 1);
        }
        ChefData chefData = chef_list[List_index[playerIndex]];
        string path = chefData.head_icon;
        ChangeChef(image, path);
    }

    //关闭面板
    void onClick_Button_Close(GameObject go, object data = null)
    {
        this.gameObject.SetActive(false);
    }

    //进入厨房
    void onClick_Button_Start(GameObject go, object data = null)
    {
        Dictionary<string, object> Params = new Dictionary<string, object>()
                {{"levelData", levelData}, };
        NotifyEvent e = new NotifyEvent(NotifyType.ENTER_KITCHEN, Params, this);
        NotifacitionCenter.GetInstance().postNotification(e);
    }
    
    //更改厨师头像
    void ChangeChef(Image image, string path)
    {
        Sprite sprite = ResourceManager.dictionary_sprite[path];
        image.sprite = sprite;
    }

    //获得玩家的头像index
    int GetIndex(int index, int min, int max)
    {
        int ret = index;
        if (index < min)
        {
            ret = max;
        }
        if (index > max)
        {
            ret = min;
        }

        return ret;
    }
}

public struct ButtonParam
{
    public int playerIndex;
    public int dir;
}
